package com.lee.thread;

/**
 * 
 * @author L+
 * 功能：建立三个线程，A线程打印10次A，B线程打印10次B,C线程打印10次C，要求线程同时运行，交替打印10次ABC
 */



public class MyThreadPrinter implements Runnable {
	private String name;
	
	private  Object prev;
	
	private Object self;

	public MyThreadPrinter(String name, Object prev, Object self) {
		super();
		this.name = name;
		this.prev = prev;
		this.self = self;
	}

	@Override
	public void run() {
		int count = 10;
		while (count>0) {
			synchronized (prev) {
				synchronized (self) {
					self.notify();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.print(name);
					count--;
				}
				
				try {
					// 释放自身对象的锁，释放CPU的控制权，进入等待状态
					prev.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		Object a = new Object();
		Object b = new Object();
		Object c = new Object();
		
		MyThreadPrinter t1 = new MyThreadPrinter("A", c, a);
		MyThreadPrinter t2 = new MyThreadPrinter("B", a, b);
		MyThreadPrinter t3 = new MyThreadPrinter("C", b, c);
		new Thread(t1).start();
		Thread.sleep(100);
		new Thread(t2).start();
		Thread.sleep(100);
		new Thread(t3).start();
				
	}
	
	
}
